/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.kungkhies.persistence.kspace;

import com.kungkhies.domain.kspace.KSpaceMessage;
import com.kungkhies.domain.kungkher.Kungkher;
import com.kungkhies.domain.kungkhie.KungkhieSummary;
import com.kungkhies.persistence.kungkher.KungkherDao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.security.access.annotation.Secured;

/**
 *
 * @author clayton
 */
public class JdbcSupportKSpaceMessageDaoImpl extends JdbcDaoSupport implements KSpaceMessageDao
{
    private static final String SQL_INSERT_KSPACE_MESSAGE = "insert into kspace (kungkher_id, messageType, isDisplayed, messageString) values (?, ?, ?, ?)";
    private static final String SQL_UPDATE_KSPACE_MESSAGE = "update kspace set kungkher_id = ?, messageType = ?, isDisplayed = ?, messageString = ?"
            + " where message_id = ?";
    private static final String SQL_SELECT_KSPACE_MESSAGE_BY_ID = "select * from kspace"
            + " where message_id = ?";
    private static final String SQL_SELECT_KSPACE_MESSAGES_BY_KUNGKHER_ID = "select message_id, messageType, timePublished, isDisplayed, messageString from kspace"
            + " where kungkher_id = ?";
    private static final String SQL_SELECT_ALL_KSPACE_MESSAGES = "select * from kspace";
    private static final String SQL_SELECT_DISPLAYABLE_KSPACE_MESSAGES_BY_KUNGKHER_ID = "select message_id, messageType, timePublished, messageString from kspace"
            + " where kungkher_id = ? and isDisplayed = 'true'";
    private static final String SQL_DELETE_KSPACE_MESSAGE = "delete * from kspace where message_id = ?";
    private static final String SQL_KSPACE_MESSAGE_IS_DISPLAYABLE_TRUE = "update kspace set isDisplayed = 'true' where message_id = ?";
    private static final String SQL_KSPACE_MESSAGE_IS_DISPLAYABLE_FALSE = "update kspace set isDisplayed = 'false' where message_id = ?";
    
    private KungkherDao kungkherDao;
    
    public JdbcSupportKSpaceMessageDaoImpl(){}
    
    
    public JdbcSupportKSpaceMessageDaoImpl(KungkherDao kungkherDao)
    {
        this.kungkherDao = kungkherDao;    
    }
    
    @Override
    public void addKSpaceMessage(KSpaceMessage kSpaceMessage)
    {
        getJdbcTemplate().update(SQL_INSERT_KSPACE_MESSAGE,
                kSpaceMessage.getKungkherId(),
                kSpaceMessage.getMessageType(),
                kSpaceMessage.isIsDisplayed(),
                kSpaceMessage.getMessageString());
                kSpaceMessage.setMessageId(queryForIdentity());
    }
    
    private long queryForIdentity()
    {
        return getJdbcTemplate().queryForLong("call identity()");
    }

    @Override
    public void saveKSpaceMessage(KSpaceMessage kSpaceMessage)
    {
        getJdbcTemplate().update(SQL_UPDATE_KSPACE_MESSAGE,
                kSpaceMessage.getKungkherId(),
                kSpaceMessage.getMessageType(),
                kSpaceMessage.isIsDisplayed(),
                kSpaceMessage.getMessageString(),
                kSpaceMessage.getMessageId());
    }

    @Override
    public KSpaceMessage getKSpaceMessageById(long messageId)
    {
        return getJdbcTemplate().queryForObject(SQL_SELECT_KSPACE_MESSAGE_BY_ID,
                new ParameterizedRowMapper<KSpaceMessage>()
                {
                    public KSpaceMessage mapRow(ResultSet rs, int rowNum) throws SQLException
                    {
                        KSpaceMessage kSpaceMessage = new KSpaceMessage();

                        kSpaceMessage.setKungkherId(rs.getLong(1));
                        kSpaceMessage.setMessageType(rs.getString(2));
                        kSpaceMessage.setTimePublished(rs.getTimestamp(3));
                        kSpaceMessage.setIsDisplayed(Boolean.parseBoolean(rs.getString(4)));
                        kSpaceMessage.setMessageString(rs.getString(5));

                        return kSpaceMessage;
                    }
                }, messageId );
    }

    @Override
    public List<KSpaceMessage> getKSpaceMessagesByKungkherId(long kungkherId)
    {
        return getJdbcTemplate().query(SQL_SELECT_KSPACE_MESSAGES_BY_KUNGKHER_ID,
                new ParameterizedRowMapper<KSpaceMessage>()
                {
                    public KSpaceMessage mapRow(ResultSet rs, int rowNum) throws SQLException
                    {
                        KSpaceMessage kSpaceMessage = new KSpaceMessage();

                        kSpaceMessage.setMessageId(rs.getLong(1));
                        kSpaceMessage.setMessageType(rs.getString(2));
                        kSpaceMessage.setTimePublished(rs.getTimestamp(3));
                        kSpaceMessage.setIsDisplayed(Boolean.parseBoolean(rs.getString(4)));
                        kSpaceMessage.setMessageString(rs.getString(5));

                        return kSpaceMessage;
                    }
                }, kungkherId);
    }

    @Override
    public List<KSpaceMessage> getKSpaceMessagesByKungkherName(String kungkerName)
    {
        return getKSpaceMessagesByKungkherId(getKungkherIdFromName(kungkerName));
    }

    @Override
    public List<KSpaceMessage> getDisplayableKSpaceMessagesByKungkherId(long kungkherId)
    {
        return getJdbcTemplate().query(SQL_SELECT_DISPLAYABLE_KSPACE_MESSAGES_BY_KUNGKHER_ID,
                new ParameterizedRowMapper<KSpaceMessage>()
                {
                    public KSpaceMessage mapRow(ResultSet rs, int rowNum) throws SQLException
                    {
                        KSpaceMessage kSpaceMessage = new KSpaceMessage();

                        kSpaceMessage.setMessageId(rs.getLong(1));
                        kSpaceMessage.setMessageType(rs.getString(2));
                        kSpaceMessage.setTimePublished(rs.getTimestamp(3));
                        kSpaceMessage.setMessageString(rs.getString(4));

                        return kSpaceMessage;
                    }
                }, kungkherId);
    }

    @Override
    public List<KSpaceMessage> getDisplayableKSpaceMessagesByKungkherName(String kungkerName)
    {
        return getDisplayableKSpaceMessagesByKungkherId(getKungkherIdFromName(kungkerName));
    }

    @Override
    public void deleteKSpaceMessage(long messageId)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void setIsDisplayedTrue(long messageId)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void setIsDisplayedFalse(long messageId)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    public long getKungkherIdFromName(String kungkherName)
    {
        return kungkherDao.getKungkherByUsername(kungkherName).getId();
    }
    
}
